<?php
/*
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */

class index_controller extends common{
    function goChat_action()
    {
        
        if ($this -> config['sy_chat_open'] == 1){
            
            $chatM  =  $this->MODEL('chat');
            $chat   =  $chatM->userinfo(array('uid'=>$this->uid,'usertype'=>$this->usertype));
            $token  =  $chatM->getToken($this->uid);
            
            $chat['yuntoken']  =  $token;
            
            echo json_encode($chat);
        }
    }
    //开始聊天，先发个信息作为开始
    function beginChat_action()
    {
        if ($this->uid && $this->usertype){
            
            $data   =  array(
                'toid'       =>  $this->uid,
                'fromid'     =>  (int)$_POST['id'],
                'tusertype'  =>  $this->usertype,
                'fusertype'  =>  $_POST['usertype'],
                'jobid'      =>  $_POST['jobid'],
                'jobtype'    =>  $_POST['jobtype'],
                'timestamp'  =>  $_POST['timestamp']
            );
            
            $chatM   =  $this->MODEL('chat');
            
            $return  =  $chatM -> beginChat($data);
            
            echo json_encode($return);
        }
    }
    //添加聊天发送记录
    function chatLog_action()
    {
        $arr     =  array(
            'toid'       =>  $_POST['to'],
            'tusertype'  =>  $_POST['totype'],
            'fromid'     =>  $this->uid,
            'fusertype'  =>  $this->usertype,
            'content'    =>  $_POST['content'],
            'timestamp'  =>  $_POST['timestamp'],
            'msgtype'    =>  $_POST['msgtype'],
            'nowid'      =>  $_POST['nowid'],
            'nowtype'    =>  $_POST['nowtype']
        );
        
        $chatM   =  $this -> MODEL('chat');
        
        $return  =  $chatM -> chatLog($arr);
        
        echo json_encode($return);die;
    }
    /**
     * 聊天记录分页
     */
    function getChatPage_action()
    {
        if ($this->uid && $this->usertype){
            
            $arr    =  array(
                'toid'       =>  $_POST['id'],
                'tusertype'  =>  $_POST['totype'],
                'fromid'     =>  $this->uid,
                'fusertype'  =>  $this->usertype,
                'page'       =>  $_POST['page']
            );
            
            $chatM   =  $this -> MODEL('chat');
            
            $return  =  $chatM -> getChatPage($arr);
            
            echo  json_encode($return);die;
        }
    }
    /**
     * 判断简历是否被下载
     */
    function getdown_action()
    {
        
        $arr   =  array(
            'eid'         =>  $_POST['eid'],
            'toid'        =>  $_POST['toid'],
            'nowtype'     =>  $_POST['nowtype'],
            'fromid'      =>  $this->uid,
            'fusertype'   =>  $this->usertype,
            'zid'         =>  $_POST['zid']
        );
        
        $chatM  =  $this->MODEL('chat');
        
        $return  =  $chatM -> comToChat($arr);
        
        echo json_encode($return);
    }
    /**
     * 发送图片信息
     */
    function uploadImage_action()
    {
        $UploadM  =  $this -> MODEL('upload');
        
        $upArr  =  array(
            'file' =>  $_FILES['file'],
            'dir'  =>  'chat'
        );
        $result  =  $UploadM->newUpload($upArr);
        
        if (!empty($result['msg'])){
            $return  =  array(
                'code'  =>  1,
                'msg'   =>  $result['msg'],
                'data'  =>  array()
            );
        }else{
            $pic  =  $result['picurl'];
            
            $result['picurl']  =  checkpic($pic);
            
            $return = array(
                'code' => 0,
                'msg' => '',
                'data' => array('url'=>$result['picurl'])
            );
        }
        echo json_encode($return);
    }
    //条件聊天时，判断个人是否有简历，并判断是否申请过该企业职位/已被企业邀请面试
    function isResume_action(){
        
        $arr  =  array(
            'uid'       =>  $this->uid,
            'usertype'  =>  $this->usertype,
            'comid'     =>  $_POST['id'],
            'jobtype'   =>  $_POST['jobtype'],
            'jobid'     =>  $_POST['jobid'],
            'nowtype'   =>  $_POST['nowtype'],
            'zid'       =>  $_POST['zid']
        );
        
        $chatM  =  $this->MODEL('chat');
        
        $return  =  $chatM -> userToChat($arr);
        
        echo json_encode($return);die;
    }
    // 收到消息即设为已读
    function setMsgStatus_action()
    {
        
        $chatM   =  $this -> MODEL('chat');
        
        $arr     =  array(
            'toid'       =>  $this->uid,
            'fromid'     =>  $_POST['id'],
            'tusertype'  =>  $this->usertype,
            'fusertype'  =>  $_POST['type'],
            'nowid'      =>  $_POST['nowid'],
            'nowtype'    =>  $_POST['nowtype']
        );
        
        $return  =  $chatM->setMsg($arr);
        
        echo json_encode($return);die;
    }
    // 不感兴趣
    function delChatLog_action()
    {
        $return  =  array();
        
        if ($this->uid && $this->usertype){
            
            $chatM  =  $this -> MODEL('chat');
            
            $chatM->delChatLog(array('toid'=>$_POST['to'],'fromid'=>$this->uid, 'tusertype'=>$_POST['type'],'fusertype'=>$this->usertype));
            
            $chatM->upFriend(array('uid'=>$this->uid,'usertype'=>$this->usertype,'fid'=>$_POST['to'],'fusertype'=>$_POST['type']),array('state'=>2));
            
        }else{
            
            $return  =  array('errcode'=>8,'msg'=>'请先登录');
        }
        echo json_encode($return);die;
    }
    // 由简历/职位详情页进入,发预定消息
    function prepare_action()
    {
        $pArr   =  array(
            'uid'       =>  $this->uid,
            'usertype'  =>  $this->usertype,
            'toid'      =>  intval($_POST['toid']),
            'totype'    =>  intval($_POST['totype'])
        );
        
        $chatM  =  $this->MODEL('chat');
        
        $list   =  $chatM -> getPrepare($pArr);
        
        echo json_encode($list);die;
    }
    // 查询邀请面试情况
    function inviteCheck_action(){
        
        $jobM  =  $this->MODEL('job');
        
        $where  =  array('uid'=>intval($_POST['id']));
        
        if (!empty($_POST['jid'])){
            
            $where['jobid']  =  intval($_POST['jid']);
        }
        
        $num      =  $jobM->getYqmsNum($where);
        
        $resumeM  =  $this->MODEL('resume');
        
        $resume   =  $resumeM->getResumeInfo(array('uid'=>intval($_POST['id'])),array('field'=>'`name`'));
        
        if ($num > 0){
            $data['msg']  =  '您已邀请其面试，前往查看邀请记录？';
        }else{
            $data['msg']  =  '';
        }
        $data['name']  =  $resume['name'];
        
        echo json_encode($data);die;
    }
    /**
     * 用户不在线，收到消息，提醒用户
     */
    function unSend_action()
    {
        $chatM   =  $this -> MODEL('chat');
        
        $arr     =  array(
            'fromid'     =>  $this->uid,
            'fusertype'  =>  $this->usertype,
            'toid'       =>  $_POST['toid'],
            'tusertype'  =>  $_POST['totype'],
            'nowtype'    =>  $_POST['nowtype']
        );
        
        $return  =  $chatM->setUnSend($arr);
    }
    /**
     * 获取聊天token
     */
    function getToken_action()
    {
        
        $chatM  =  $this->MODEL('chat');
        
        $token  =  $chatM->getToken($this->uid);
        
        $chat['yuntoken']  =  $token['token'];
        
        echo json_encode($chat);
    }
}
?>